<?php
namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\facade\App;
use think\facade\Env;

class ExportTask extends Command
{
    protected function configure() {
        $this->setName('ExportTask')->setDescription("导出任务");
    }

    protected function execute(Input $input, Output $output) {
        ini_set('max_execution_time', 0);
        set_time_limit(0);
        ini_set('memory_limit','20480M');

        $tasks = Db::name('export_task')->where('status',0)->select();
        $cur_time = time();
        foreach ($tasks as $task) {
            if (empty($task['task_sn'])) {
                continue;
            }
            $folder = 'C:\phpstudy_pro\WWW\tyoa\public\task/';
            $fields = json_decode($task['fields'],true);
            $maps = json_decode($task['params'],true);
            if (!empty($maps['whereor'])) {
                foreach ($maps['whereor'] as $whereor) {
                    $maps[] = Db::raw(implode(' or ',$whereor));
                }
                unset($maps['whereor']);
            }
            try {
                $list = (new \app\admin\controller\Order())->exportData($maps);
                // $list = json_decode(file_get_contents('C:/111.txt'),true);
                // file_put_contents('C:/111.txt',json_encode($list));

                $data = [];
                // $data[] = implode(',' , $fields);
                $data[] = $fields;

                // $tmps = [];
                // foreach ($fields as $field) {
                //     $tmps[] = array_column($list, $field);
                // }
                // $num = count($tmps[0]);
                // for ($i=0; $i < $num; $i++) { 
                //     $tmp = array_column($tmps, $i);
                //     $data[] = $tmp;
                // }


                foreach ($list as $k => $vo) {
                    $tmp = [];
                    foreach ($fields as $field) {
                        $tmp[] = $vo[$field];
                    }
                    $data[] = $tmp;
                }
                $result = saveExcel($folder.$task['task_sn'].'.xlsx',$data);

                // $lists_str = implode( "\r\n" , $data );
                // file_put_contents($folder.$task['task_sn'].'.csv', $lists_str);

                Db::name('export_task')->where([
                    ['status','=',0],['id','=',$task['id']]
                ])->update([
                    'utime'=>time(),
                    // 'remark'=>$result['msg'],
                    // 'status'=>$result['status'] == 0 ? -1 : 1
                    'status'=>1
                ]);
            } catch (\Exception $e) {
                Db::name('export_task')->where([
                    ['status','=',0],['id','=',$task['id']]
                ])->update([
                    'utime'=>time(),
                    'remark'=>$e->getMessage(),
                    // 'status'=>$result['status'] == 0 ? -1 : 1
                    'status'=>-1
                ]);
            }
            
        }
      
    }
}